/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.pps.web.bean.course;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import javax.faces.context.FacesContext;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ReverseComparator;
import vn.com.pps.entities.Course;
import vn.com.pps.entities.CoursePupilDetail;
import vn.com.pps.entities.CoursePupilFee;
import vn.com.pps.web.bean.common.JasperReportBean;
import vn.com.pps.web.common.component.table.LazyCacheModel;
import vn.com.pps.web.ui.CoursePupilFeeTableItem;
import vn.com.pps.web.ui.CoursePupilTableItem;

/**
 *
 * @author Dai
 */
public class CoursePupilBean extends LazyCacheModel implements Serializable {

    private int courseID;
    private int pupilID;
    private int coursePupilID;
    private int coursePupilFeeID;
    private int rowCount = 0;
    private BigDecimal fee;
    private int pupilActionType = 0;

    private ArrayList<CoursePupilTableItem> coursePupilItems;
    public ArrayList<CoursePupilFeeTableItem> coursePupilFeeTableItem;
    

    @Override
    public void sort(String pm_column, boolean pm_ascending) {
        Comparator<CoursePupilTableItem> bc = null;
        if (pm_ascending) {
            bc = new BeanComparator(pm_column);
        } else {
            bc = new BeanComparator(pm_column, new ReverseComparator(new ComparableComparator()));
        }
        Collections.sort(getPageItems(), bc);
    }

    @Override
    public void loadData() {
        System.out.println("@Load Pupil by course");
        rowCount = getServiceLocator().getCourseService().countPupilByCourse(courseID);
        coursePupilItems = getServiceLocator().getCourseService().getPupilByCourse(getTargetPage(), getMaximumPageSize(), courseID);
        setPageItems(coursePupilItems);
        setRowCount(rowCount);

    }

    public int getCourseID() {
        return courseID;
    }

    public void setCourseID(int courseID) {
        this.courseID = courseID;
    }

    public int getCoursePupilID() {
        return coursePupilID;
    }

    public void setCoursePupilID(int coursePupilID) {
        this.coursePupilID = coursePupilID;
    }

    public BigDecimal getFee() {
        return fee;
    }

    public void setFee(BigDecimal fee) {
        this.fee = fee;
    }
    
    public String preAddPupilToCourse(){
        return "success";
    }

    public String editPupilCourse() {
        pupilActionType = 1;
        FacesContext facesContext = FacesContext.getCurrentInstance();
        CoursePupilDetail coursePupilDetail
                = (CoursePupilDetail) facesContext.getApplication()
                .createValueBinding("#{coursePupilDetail}").getValue(facesContext);
        CoursePupilDetail tmp = getServiceLocator().getCourseService().getCoursePupilDetail(coursePupilID);
        coursePupilDetail.setFirstName(tmp.getFirstName());
        coursePupilDetail.setLastName(tmp.getLastName());
        coursePupilDetail.setMobile(tmp.getMobile());
        coursePupilDetail.setRegistedOn(tmp.getRegistedOn());
        coursePupilDetail.setParentName(tmp.getParentName());
        coursePupilDetail.setStatus(tmp.getStatus());
        courseID = tmp.getCourseID();
        pupilID = tmp.getPupilID();
        coursePupilFeeTableItem = getServiceLocator().getCourseService().getCourseFees(courseID, pupilID);
        return "success";
    }

    public String selectPupilCourse() {
        System.out.println("@selectPupilCourse");
        pupilActionType = 2;
        FacesContext facesContext = FacesContext.getCurrentInstance();
        CoursePupilDetail coursePupilDetail
                = (CoursePupilDetail) facesContext.getApplication()
                .createValueBinding("#{coursePupilDetail}").getValue(facesContext);
        CoursePupilDetail tmp = getServiceLocator().getCourseService().getCoursePupilDetail(coursePupilID);
        coursePupilDetail.setFirstName(tmp.getFirstName());
        coursePupilDetail.setLastName(tmp.getLastName());
        coursePupilDetail.setMobile(tmp.getMobile());
        coursePupilDetail.setRegistedOn(tmp.getRegistedOn());
        coursePupilDetail.setParentName(tmp.getParentName());
        coursePupilDetail.setStatus(tmp.getStatus());
        coursePupilDetail.setId(tmp.getId());
        
        courseID = tmp.getCourseID();
        pupilID = tmp.getPupilID();
        coursePupilDetail.setPupilID(pupilID);
        coursePupilDetail.setCourseID(courseID);
        CourseManageBean courseManageBean
                = (CourseManageBean) facesContext.getApplication()
                .createValueBinding("#{courseManageBean}").getValue(facesContext);
        String criterial=" tblcourses.STATUS <> 'C' AND tblcourses.ID <> "+courseManageBean.getSelectedID();
        courseManageBean.setCriterial(criterial);
        courseManageBean.loadData();
        return "success";
    }
    
    public void print() throws Exception{
        HashMap parameters = new HashMap();
        parameters.put("pCourseID", courseID);
        Course course = getServiceLocator().getCourseService().getCourse(courseID);
        parameters.put("pCourseName", course.getCourseName());
        parameters.put("pTimeFrame", course.getTimeFrame());
        parameters.put("pStartDate", course.getStartedDate());
        parameters.put("pCourseTeacherName", "Trần Minh Phương");
        JasperReportBean.printJRReport(parameters, JasperReportBean.JRTEMPLATE_PUPILLIST);
    }

    public ArrayList<CoursePupilFeeTableItem> getCoursePupilFeeTableItem() {
        return coursePupilFeeTableItem;
    }

    public void setCoursePupilFeeTableItem(ArrayList<CoursePupilFeeTableItem> coursePupilFeeTableItem) {
        this.coursePupilFeeTableItem = coursePupilFeeTableItem;
    }

    public int getCoursePupilFeeID() {
        return coursePupilFeeID;
    }

    public void setCoursePupilFeeID(int coursePupilFeeID) {
        this.coursePupilFeeID = coursePupilFeeID;
    }

    public void updateCourseFee() {
        System.out.println("Amount:" + fee);
        System.out.println("coursePupilFeeID :" + coursePupilFeeID);
        CoursePupilFee courseFee = new CoursePupilFee();
        courseFee.setId(coursePupilFeeID);
        courseFee.setPaidStatus("F");
        courseFee.setAmount(fee);
        java.util.Date date = new java.util.Date();
        courseFee.setPaidOn(new Timestamp(date.getTime()));
        getServiceLocator().getCourseService().update(courseFee);
        coursePupilFeeTableItem = getServiceLocator().getCourseService().getCourseFees(courseID, pupilID);
    }

    public void exportReciept() throws Exception{
        HashMap parameters = new HashMap();
        parameters.put("pCashName", "pCashName Value");
        parameters.put("pAddress", "pAddress Value");
        parameters.put("pMonth", "pMonth Value");
        parameters.put("pCourseName", "pCourseName Value");
        parameters.put("pAmount", "pAmount Value");
        parameters.put("pAmountString", "pAmountString Value");
        JasperReportBean.printJRReport(parameters, JasperReportBean.JRTEMPLATE_BILL);
    }

    public int getPupilID() {
        return pupilID;
    }

    public void setPupilID(int pupilID) {
        this.pupilID = pupilID;
    }

    public int getPupilActionType() {
        return pupilActionType;
    }

    public void setPupilActionType(int pupilActionType) {
        this.pupilActionType = pupilActionType;
    }

}
